package com;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 实习管理系统主启动类
 * 
 * @author admin
 * @version 1.0
 * @date 2025-03-22
 */
@SpringBootApplication
@MapperScan(basePackages = {"com.dao"})
public class SpringbootSchemaApplication extends SpringBootServletInitializer {

	/**
     * 程序主入口
     * 
     * @param args 启动参数
     */
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		
		System.out.println("==========================================");
		System.out.println("  实习管理系统启动中，请稍后...");
		System.out.println("  版本日期：2025-03-22");
		System.out.println("==========================================");
		
		// 使用自定义参数优化启动
		SpringApplication app = new SpringApplication(SpringbootSchemaApplication.class);
		ConfigurableApplicationContext context = app.run(args);
		
		// 启动完成后展示关键信息
		Environment env = context.getEnvironment();
		String ip = "localhost";
		try {
			ip = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
		String port = env.getProperty("server.port");
		String path = env.getProperty("server.servlet.context-path");
		if (path == null) {
			path = "";
		}
		
		long endTime = System.currentTimeMillis();
		float seconds = (endTime - startTime) / 1000F;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		System.out.println("\n----------------------------------------------------------");
		System.out.println("  实习管理系统启动成功！");
		System.out.println("  后台访问: http://127.0.0.1:" + port + path + "/admin/dist/index.html#/login");
		System.out.println("  前台访问: http://localhost:" + port + path + "/front/pages/login/login.html");
		System.out.println("  外部访问: http://" + ip + ":" + port + path);
		System.out.println("  启动耗时: " + seconds + "秒");
		System.out.println("  启动时间: " + dateFormat.format(new Date()));
		System.out.println("  系统版本: 2025-03-22");
		System.out.println("----------------------------------------------------------\n");
	}
	
	/**
     * 配置Spring应用构建器
     * 
     * @param applicationBuilder Spring应用构建器
     * @return 配置后的Spring应用构建器
     */
	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
        return applicationBuilder.sources(SpringbootSchemaApplication.class);
    }
}
